package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.GoodImage;
import com.wuming.house.model.House;
import com.wuming.house.model.HouseImg;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.GoodImageService;
import com.wuming.house.service.HouseImgService;
import com.wuming.house.service.HouseService;
import com.wuming.house.service.SecondhouseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/9/5 16:40
 * @Description:
 */
@Controller
@RequestMapping("/web/houseImg")
public class HouseImgController extends BaseController {
    @Autowired
    private HouseImgService houseImgService;

    @Autowired
    private GoodImageService goodImageService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private SecondhouseService secondhouseService;

    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam Long houseId, @RequestParam(defaultValue = "1") Integer houseType, @RequestParam(defaultValue = "1") int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize) {
        logger.info("准备查询楼盘id:{}的图片信息", houseId);
        try {
            List<HouseImg> houseImgs = houseImgService.getHouseImgByHouseId(houseId, houseType, pageIndex, pageSize);
            int total = houseImgService.getTotal(houseId, houseType);
            return CommonResult.createSuccessResult(houseImgs, total, "查询楼盘图片信息成功");
        } catch (Exception e) {
            logger.error("查询楼盘图片信息失败,失败信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询楼盘图片信息失败");
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        logger.info("准备删除id为:{}的楼盘图片", id);
        try {
            houseImgService.deleteById(id);
            return new JsonResult(null, "删除楼盘图片成功", true);
        } catch (Exception e) {
            logger.error("删除楼盘图片失败,失败信息:{}", e.getMessage());
            return new JsonResult(null, "删除楼盘图片失败", false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, @RequestParam Long houseId, @RequestParam Integer houseType) {
        try {
            String path = this.uploadImage(file, "houseImg", houseType);
            Date date = new Date();
            GoodImage goodImage = new GoodImage();
            goodImage.setImagePath(path);
            goodImage.setCreateDatetime(date);
            goodImageService.saveImage(goodImage);
            HouseImg houseImg = new HouseImg();
            houseImg.setHouseId(houseId);
            houseImg.setCreateDatetime(date);
            houseImg.setPicId(goodImage.getId().longValue());
            houseImg.setPicUrl(path);
            houseImg.setHouseType(houseType);
            houseImgService.saveHouseImg(houseImg);
            if (houseType.intValue() == HouseType.NEW_HOUSE.getValue().intValue()) {
                int houseImgCount = houseImgService.getTotal(houseId, HouseType.NEW_HOUSE.getValue().intValue());
                if (houseImgCount == 1) {
                    House house = houseService.getHouseById(houseId);
                    house.setMainPicUrl(path);
                    houseService.modifyHouse(house);
                }
            } else {
                int houseImgCount = houseImgService.getTotal(houseId, HouseType.SECOND_HOUSE.getValue().intValue());
                if (houseImgCount == 1) {
                    Secondhouse sh = secondhouseService.getSecondhouseById(houseId);
                    sh.setMainPicUrl(path);
                    secondhouseService.modifySecondhouse(sh);
                }
            }
            return new JsonResult(goodImage.getId(), "上传成功", true);
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult("", "上传失败", false);
    }
}
